<template>
  <j-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    :ok-button-props="{ props: { disabled: disableSubmit } }"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <!-- 主表单区域 -->
      <a-form :form="form">
        <a-row>
          <!--<a-col :span="12">-->
            <!--<a-form-item label="提交人" :labelCol="labelCol" :wrapperCol="wrapperCol">-->
              <!--<j-dict-select-tag-->
                <!--:disabled="disableSubmit"-->
                <!--type="list"-->
                <!--v-decorator="['partyId', validatorRules.partyId]"-->
                <!--:trigger-change="true"-->
                <!--dictCode="Person,name,partyId,typeId = EMPLOYEE"-->
                <!--placeholder="请选择提交人"-->
              <!--/>-->
            <!--</a-form-item>-->
          <!--</a-col>-->
          <a-col :span="12">
            <a-form-item label="采购类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                :disabled="disableSubmit"
                type="list"
                v-decorator="['typeId', validatorRules.typeId]"
                :trigger-change="true"
                dictCode="DataDictionary,description,dictionaryCode, dictionaryTypeId =COSTREQUEST_TYPE"
                placeholder="请选择采购类型"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="成本中心" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-search-select-tag placeholder="请选择成本中心" @change="handleCategoryChange"
                                   v-decorator="['costCode', validatorRules.costCode]" :disabled="disableSubmit"
                                   dict="DataDictionary,description,dictionaryCode, dictionaryTypeId =COSTCENTER3_TYPE"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="申请事由" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['description', validatorRules.description]"
                placeholder="请输入申请事由"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
             <a-form-item label="附件" :labelCol="labelCol" :wrapperCol="wrapperCol">
               <j-upload :disabled="disableSubmit" :isMultiple="false" v-decorator="['uploadPath']" :trigger-change="true"></j-upload>
             </a-form-item>
          </a-col>


        </a-row>
      </a-form>

      <!-- 子表单区域 -->
      <a-tabs v-model="activeKey" @change="handleChangeTabs">
        <a-tab-pane tab="采购明细项表" :key="refKeys[0]" :forceRender="true">
          <j-editable-table
            :disabled="disableSubmit"
            :ref="refKeys[0]"
            :tableName="refKeys[0]"
            :loading="clmPurchaseItemTable.loading"
            :columns="clmPurchaseItemTable.columns"
            :dataSource="clmPurchaseItemTable.dataSource"
            :maxHeight="300"
            :rowNumber="true"
            :rowSelection="!disableSubmit"
            :actionButton="!disableSubmit"
          />
        </a-tab-pane>
      </a-tabs>
    </a-spin>
  </j-modal>
</template>

<script>
  import pick from 'lodash.pick'
  import {FormTypes, getRefPromise} from '@/utils/JEditableTableUtil'
  import {JEditableTableMixin} from '@/mixins/JEditableTableMixin'
  import {validateDuplicateValue} from '@/utils/util'
  import JUpload from '@/components/jeecg/JUpload'
  import JDate from '@/components/jeecg/JDate'
  import JDictSelectTag from '@/components/dict/JDictSelectTag'
  import JSearchSelectTag from '@/components/dict/JSearchSelectTag'
  import JCategorySelect from '@/components/jeecg/JCategorySelect'
  import { getAction } from '@/api/manage'
  export default {
    name: 'ClmPaymentOrderModal',
    mixins: [JEditableTableMixin],
    components: {
      JUpload,
      JDictSelectTag,
      JSearchSelectTag,
      JDate,
      JCategorySelect,
    },
    data() {
      return {
        disableSubmit: false,
        labelCol: {
          xs: {span: 24},
          sm: {span: 6},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16},
        },
        labelCol2: {
          xs: {span: 24},
          sm: {span: 3},
        },
        wrapperCol2: {
          xs: {span: 24},
          sm: {span: 20},
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
          partyId: {
            rules: [{required: true, message: '请选择提交人!'}],
          },
          typeId: {
            rules: [{required: true, message: '请选择付款类型!'}],
          },
          costCode: {
            rules: [{required: true, message: '请选择成本中心!'}],
          },
          description: {
            rules: [{required: true, message: '请输入申请事由!'}],
          },
        },
        refKeys: ['clmPurchaseItem'],
        tableKeys: ['clmPurchaseItem'],
        activeKey: 'clmPurchaseItem',
        // 报销明细项表
        clmPurchaseItemTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '物品名称',
              key: 'scomment',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [],
            },
            {
              title: '型号或规格',
              key: 'model',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请选择${title}',
              defaultValue: '',
              validateRules: [],
            },
            {
              title: '数量',
              key: 'quantity',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请选择${title}',
              defaultValue: '',
              validateRules: [],
            },
            {
              title: '金额',
              key: 'money',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{required: true, message: '${title}不能为空'}],
            },
         
            {
              title: '备注',
              key: 'remark',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [],
            },
          ],
        },
        url: {
          add: '/online/special/api/createClmPurchaseRequest',
          edit: '/online/special/api/updateClmPurchaseRequest',
          get: '/online/special/api/getClmPurchaseRequest',
          getEnumId: '/online/dict/getDictItems/',
        },
      }
    },
    methods: {
      getAllTable() {
        let values = this.tableKeys.map((key) => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      async editAfter() {
        let fieldval = pick(
          this.model,
          'partyId',
          'costCode',
          'typeId',
          'description',
          'uploadPath',
        )
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldval)
        })
        // 加载子表数据
        if (this.model.itemList) {
          this.clmPurchaseItemTable.dataSource = JSON.parse(this.model.itemList)
        }
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          itemList: JSON.stringify(allValues.tablesValue[0].values),
        }
        console.log(allValues)
      },
      validateError(msg) {
        if (msg) {
          this.isHandleOk = false
        }
        this.$message.error(msg)
      },
      popupCallback(row) {
        this.form.setFieldsValue(
          pick(
            row,
             'partyId',
             'costCode',
             'typeId',
             'description',
             'uploadPath',
          )
        )
      },
      handleCategoryChange(value, backObj) {
        this.form.setFieldsValue(backObj)
      },
    },
  }
</script>

<style scoped>
</style>
